﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using NewtifyMe.DAL;
using NewtifyMe.DataTypes;
using Types = NewtifyMe.DataTypes;
using NewtifyMe.Business;
using Const = NewtifyMe.Consts;

namespace NewtifyMe.Business
{
    public class FeedSource
    {

        /// <summary>
        /// Vitor Viana  - 12-03-2012
        /// 
        /// 
        /// </summary>
        /// <returns></returns>
        public static List<Types.FeedSource> GetAllFeedSource()
        {
            try
            {
                List<Types.FeedSource> list = new List<Types.FeedSource>();

                using (NewtifyMeEntities data = new NewtifyMeEntities())
                {

                    ObjectResult<spSelect_GetAllFeedSources_Result> feedSource = data.spSelect_GetAllFeedSources();


                    Types.FeedSource feed; 
                    foreach (var item in feedSource)
                    {
                        feed = new Types.FeedSource()
                                   {
                                       IdFeedSource =  item.IdFeedSource,
                                       IdParent =   item.IdParent.HasValue ? item.IdParent.Value : SqlInt32.Null,
                                       CodFeedSource =  item.CodFeedSource,
                                       Descritption = item.Descritpion,
                                       Url =  item.Url,
                                       Image =  item.Image,
                                       Info = new Types.SysInformation()
                                           {
                                               SysStatus =  item.SYS_STATUS
                                           }
                                   };

                       feed.FeedSourceSkeleton = GetFeedSourceSkeleton(feed);


                       list.Add(feed);
                    }
                }

                return list;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        /// <summary>
        /// Get total skeleton for specific feed source
        /// </summary>
        /// <param name="feedSource"></param>
        /// <returns></returns>
        public static FeedTotalSkeleton GetFeedSourceSkeleton( Types.FeedSource feedSource)
        {
            try
            {
                FeedTotalSkeleton ret = new FeedTotalSkeleton();
                spSelect_FeedSourceSkeleton_Result item=null;

                Types.FeedSkeletonItem skeleton;

                using (NewtifyMe.DAL.NewtifyMeEntities db = new NewtifyMe.DAL.NewtifyMeEntities())
                {
                    ObjectResult<spSelect_FeedSourceSkeleton_Result> list =
                        db.spSelect_FeedSourceSkeleton(feedSource.IdFeedSource.Value);


                    foreach (var x in list)
                    {
                        item = x;

                        //Lang
                       if(   x.CodSkeletonPart != null && x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_LANGUAGE))
                              
                            ret.Language = new FeedSkeletonItem()
                                               {
                                                   IdSkeleton = item.idSkeleton,
                                                   CodSkeleton = item.CodSkeleton,
                                                   CodSkeletonPart = item.CodSkeletonPart,
                                                   ValueHold = item.ValueHold.Trim(),
                                                   Description = item.Description,
                                                   IsMultiple = item.IsMultiple,
                                                   Namespace = item.Namespace.Trim(),
                                                   Attribute = item.Attr,
                                                   Info = new SysInformation()
                                                              {
                                                                  SysStatus = item.SYS_STATUS
                                                              }
                                               };

                        //LastBuildDate
                       if(
                                x.CodSkeletonPart != null && x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_LAST_BUILD)
                          )
                        ret.LastBuild = new FeedSkeletonItem()
                                            {
                                                IdSkeleton = item.idSkeleton,
                                                CodSkeleton = item.CodSkeleton,
                                                CodSkeletonPart = item.CodSkeletonPart,
                                                ValueHold = item.ValueHold.Trim(),
                                                Description = item.Description,
                                                IsMultiple = item.IsMultiple,
                                                Namespace = item.Namespace.Trim(),
                                                Attribute = item.Attr,
                                                Info = new SysInformation()
                                                           {
                                                               SysStatus = item.SYS_STATUS
                                                           }
                                            };

                        //Item
                       if( x.CodSkeletonPart != null && x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM))
                            ret.Item = new FeedSkeletonItem()
                                           {
                                               IdSkeleton = item.idSkeleton,
                                               CodSkeleton = item.CodSkeleton,
                                               CodSkeletonPart = item.CodSkeletonPart,
                                               ValueHold = item.ValueHold.Trim(),
                                               Description = item.Description,
                                               IsMultiple = item.IsMultiple,
                                               Namespace = item.Namespace.Trim(),
                                               Attribute = item.Attr,
                                               Info = new SysInformation()
                                                          {
                                                              SysStatus = item.SYS_STATUS
                                                          }
                                           };

                        //Item Title
                       if(
                                x.CodSkeletonPart != null && x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM_Title))
                        ret.ItemTitle = new FeedSkeletonItem()
                                            {
                                                IdSkeleton = item.idSkeleton,
                                                CodSkeleton = item.CodSkeleton,
                                                CodSkeletonPart = item.CodSkeletonPart,
                                                ValueHold = item.ValueHold.Trim(),
                                                Description = item.Description,
                                                IsMultiple = item.IsMultiple,
                                                Namespace = item.Namespace.Trim(),
                                                Attribute = item.Attr,
                                                Info = new SysInformation()
                                                           {
                                                               SysStatus = item.SYS_STATUS
                                                           }
                                            };

                        //Item LINK
                        if( x.CodSkeletonPart != null && x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM_LINK))
                            ret.ItemLink = new FeedSkeletonItem()
                                               {
                                                   IdSkeleton = item.idSkeleton,
                                                   CodSkeleton = item.CodSkeleton,
                                                   CodSkeletonPart = item.CodSkeletonPart,
                                                   ValueHold = item.ValueHold.Trim(),
                                                   Description = item.Description,
                                                   IsMultiple = item.IsMultiple,
                                                   Namespace = item.Namespace.Trim(),
                                                   Attribute = item.Attr,
                                                   Info = new SysInformation()
                                                              {
                                                                  SysStatus = item.SYS_STATUS
                                                              }
                                               };

                        //Item CATEGORY
                        if(
                                x.CodSkeletonPart != null &&
                                x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM_CATEGORY))

                                ret.ItemCategory = new FeedSkeletonItem()
                                                       {
                                                           IdSkeleton = item.idSkeleton,
                                                           CodSkeleton = item.CodSkeleton,
                                                           CodSkeletonPart = item.CodSkeletonPart,
                                                           ValueHold = item.ValueHold.Trim(),
                                                           Description = item.Description,
                                                           IsMultiple = item.IsMultiple,
                                                           Namespace = item.Namespace.Trim(),
                                                           Attribute = item.Attr,
                                                           Info = new SysInformation()
                                                                      {
                                                                          SysStatus = item.SYS_STATUS
                                                                      }
                                                       };

                        //Item PublicationDate
                        if(
                                x.CodSkeletonPart != null &&
                                x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM_PUBLICATION_DATE))
                            ret.ItemPublicationDate = new FeedSkeletonItem()
                                                          {
                                                              IdSkeleton = item.idSkeleton,
                                                              CodSkeleton = item.CodSkeleton,
                                                              CodSkeletonPart = item.CodSkeletonPart,
                                                              ValueHold = item.ValueHold.Trim(),
                                                              Description = item.Description,
                                                              IsMultiple = item.IsMultiple,
                                                              Namespace = item.Namespace.Trim(),
                                                              Attribute = item.Attr,
                                                              Info = new SysInformation()
                                                                         {
                                                                             SysStatus = item.SYS_STATUS
                                                                         }
                                                          };


                        //Item PublicationDate
                        if(
                                x.CodSkeletonPart != null &&
                                x.CodSkeletonPart.Equals(Const.ParseFeeds.CODE_ITEM_CONTENT))
                            ret.ItemDescription = new FeedSkeletonItem()
                                                      {
                                                          IdSkeleton = item.idSkeleton,
                                                          CodSkeleton = item.CodSkeleton,
                                                          CodSkeletonPart = item.CodSkeletonPart,
                                                          ValueHold = item.ValueHold.Trim(),
                                                          Description = item.Description,
                                                          IsMultiple = item.IsMultiple,
                                                          Namespace = item.Namespace.Trim(),
                                                          Attribute = item.Attr,
                                                          Info = new SysInformation()
                                                                     {
                                                                         SysStatus = item.SYS_STATUS
                                                                     }
                                                      };
                    }

                    return ret;

                }
            }
            catch (Exception ex)
            {
                
                throw ex;
            }

        }

    }
}
